package com.yangye.springclouddemorabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author yangye
 * @date 2020/08/19
 */
@Configuration
public class RabbitmqConfig {

	@Bean
	public DirectExchange directExchange() {
		return new DirectExchange("yangye-direct-exchange");
	}

	@Bean
	public Queue directQueue() {
		return new Queue("yangye-direct-queue");
	}

	@Bean
	public Binding directBinding() {
		return BindingBuilder.bind(directQueue()).to(directExchange()).with("yangye-direct-routing-key");
	}


	private final String dlxExchangeName = "yangye-dlx-exchange";
	private final String dlxRoutingKey = "yangye-dlx-routing-key";

	/**
	 * 过期队列，TTL时间后进入死信队列
	 *
	 * @return
	 */
	@Bean
	public Queue delayQueue() {
		return QueueBuilder.durable("yangye-delay-queue")
				.deadLetterExchange(dlxExchangeName)// 死信队列交换机
				.deadLetterRoutingKey(dlxRoutingKey)// 死信队列routing-key
				.ttl(3000)
				.build();
	}

	@Bean
	public DirectExchange dlxExchange() {
		return new DirectExchange(dlxExchangeName);
	}

	@Bean
	public Queue dlxQueue() {
		return new Queue("dlx-queue");
	}

	@Bean
	public Binding dlxBinding() {
		return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(dlxRoutingKey);
	}
}
